//
//  XDGameMgrViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDGameMgrViewController.h"
#import "XDDataPicker.h"
#import "XDVideoMgrModel.h"
@interface XDGameMgrViewController ()
//背景view
@property (weak, nonatomic) IBOutlet UIView *bgView;

//次数
@property (weak, nonatomic) IBOutlet UILabel *timesLabel;

//时间
@property (weak, nonatomic) IBOutlet UIButton *totalTimeBtn;

//开始时间
@property (weak, nonatomic) IBOutlet UIButton *startTimeBtn;

//结束时间
@property (weak, nonatomic) IBOutlet UIButton *endTimeBtn;


/** 次数 */
@property(nonatomic,assign) NSInteger times;


@property(nonatomic,strong) XDDataPicker *datePicker;


/** 判断是开始时间还是结束时间 */
@property(nonatomic,assign) BOOL isStartTime;
/** 开始时间 */
@property(nonatomic,copy) NSString *startTime;
/** 结束时间 */
@property(nonatomic, copy) NSString *endTime;

@end

static NSInteger datePickerHeight = 200;

@implementation XDGameMgrViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = HUDBgColor;
    
    [self initDatePicker];
    
    [self initData];
}


-(void)initDatePicker{
    
    _datePicker = [[XDDataPicker alloc]init];
    _datePicker.frame = CGRectMake(0, ScreenH, ScreenW, datePickerHeight);
    [self.view addSubview:_datePicker];
    
    WeakSelf(weakself)
    _datePicker.sureBlock = ^(NSString *time){
        
#warning 时间选择判断 开始时间和结束时间的大小
        if (weakself.isStartTime) { //开始时间
            
            weakself.startTime = time;
            [weakself.startTimeBtn setTitle:time forState:UIControlStateNormal];
            
        }else{  //结束时间
            
            weakself.endTime = time;
            [weakself.endTimeBtn setTitle:time forState:UIControlStateNormal];
        }
        [weakself hideDatePicker];
    };
    
    _datePicker.cancelBlock = ^(){
        
        [weakself hideDatePicker];
    };
}

//初始化控件数据
-(void)initData{
    self.timesLabel.text =  [NSString stringWithFormat:@"%@",self.videoMgrModel.time];
    
    [_totalTimeBtn setTitle:[NSString stringWithFormat:@"%@次",self.videoMgrModel.time] forState:UIControlStateNormal];
    
    [_startTimeBtn setTitle:[NSString stringWithFormat:@"%@",self.videoMgrModel.start_time] forState:UIControlStateNormal];
    
    [_endTimeBtn setTitle:[NSString stringWithFormat:@"%@",self.videoMgrModel.end_time] forState:UIControlStateNormal];
    
    _times = [self.videoMgrModel.time integerValue];
    _startTime = [NSString stringWithFormat:@"%@",self.videoMgrModel.start_time];
    _endTime = [NSString stringWithFormat:@"%@",self.videoMgrModel.end_time];
}



//减少次数
- (IBAction)cutdownBtn:(id)sender {
    
    if (_times-1 < 0) return;
    _times -= 1;
    _timesLabel.text = [NSString stringWithFormat:@"%ld",_times];
    [_totalTimeBtn setTitle:[NSString stringWithFormat:@"%ld次",_times] forState:UIControlStateNormal];
}

//增加次数
- (IBAction)addBtn:(id)sender {
    
    _times += 1;
    _timesLabel.text = [NSString stringWithFormat:@"%ld",_times];
    [_totalTimeBtn setTitle:[NSString stringWithFormat:@"%ld次",_times] forState:UIControlStateNormal];
}

//开始时间
- (IBAction)startTimeBtn:(id)sender {
    self.isStartTime = YES;
    [self showDatePicker];
}

//结束时间
- (IBAction)endTime:(id)sender {
    self.isStartTime = NO;
    [self showDatePicker];
}

//确定按钮
- (IBAction)sureBtn:(id)sender {
    [self hideDatePicker];
    [self setGameMgrTimeNetwork];
}

//取消按钮
- (IBAction)cancelBtn:(id)sender {
    [self hideDatePicker];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//显示datePicker
-(void)showDatePicker{
    
    [UIView animateWithDuration:0.35 animations:^{
        _datePicker.frame = CGRectMake(0, ScreenH - 64 - datePickerHeight, ScreenW, datePickerHeight);
        CGFloat offset_y = _bgView.y - 50;
        _bgView.frame = CGRectMake(_bgView.x, offset_y, _bgView.width, _bgView.height);
        
    }];
}

//隐藏datePicker
-(void)hideDatePicker{
    
    [UIView animateWithDuration:0.35 animations:^{
        
        self.datePicker.frame = CGRectMake(0, ScreenH, ScreenW, datePickerHeight);
        CGFloat offset_y = _bgView.y + 50;
        _bgView.frame = CGRectMake(_bgView.x, offset_y, _bgView.width, _bgView.height);
    } completion:^(BOOL finished) {
        
    }];
}


-(void)setGameMgrTimeNetwork{
    
    NSLog(@"%ld %@ %@",_times,_startTime,_endTime);
    
    NSString *url = BaseURL(@"yipark.do?action=updateLimit");
    
    NSDictionary *dict = @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,
                           @"type":@"2",
                           @"time":@(_times),
                           @"start_time":_startTime,
                           @"end_time":_endTime};
    
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        
        NSLog(@"%@",responseObj);
        [MBProgressHUD showSuccess:@"设置成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
}
@end
